package com.augurit.common.workdayholiday.web;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.workdayholiday.model.Time;
import com.augurit.common.workdayholiday.service.IWhRegularWorkTimeService;
import com.augurit.common.workdayholiday.web.form.WhRegularWorkTimeForm;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "wh-regular-work-time.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/common/workdayholiday/RegularWorkTimeSet.jsp")
})
public class WhRegularWorkTimeAction extends ExtBaseAction implements ModelDriven<WhRegularWorkTimeForm>{

	@Autowired
	private IWhRegularWorkTimeService whWorkhoursService;

	private WhRegularWorkTimeForm form = new WhRegularWorkTimeForm();
	private Long id;
	private Page<WhRegularWorkTimeForm> page = new Page<WhRegularWorkTimeForm>(10);

	/**
	 * 分页查询
	 */
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		whWorkhoursService.search(page, form);
		this.extRenderGridJson(page);
		return null;
	}

	
	/**
	 * 设置工作日和工作时间
	 */
	public String setRegularWorkTime() throws Exception{
		//清空原来的常规工作时间设置
		whWorkhoursService.clear();
		
		for(int i = 0; i < form.getDayOfWeeks().length; i++){
			setEachDayWorkTime(i);
		}
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 为每个工作日设置工作时间段
	 * @param dayOfWeek
	 * @throws Exception
	 */
	private void setEachDayWorkTime(int dayOfWeek) throws Exception{
		WhRegularWorkTimeForm curForm = new WhRegularWorkTimeForm();
		curForm.setWorkday(form.getDayOfWeeks()[dayOfWeek]);
		
		curForm.setAmStartHour(Time.detachHours(form.getAmStartTimes()[dayOfWeek]));
		curForm.setAmStartMinute(Time.detachMinutes(form.getAmStartTimes()[dayOfWeek]));
		curForm.setAmEndHour(Time.detachHours(form.getAmEndTimes()[dayOfWeek]));
		curForm.setAmEndMinute(Time.detachMinutes(form.getAmEndTimes()[dayOfWeek]));
		
		curForm.setPmStartHour(Time.detachHours(form.getPmStartTimes()[dayOfWeek]));
		curForm.setPmStartMinute(Time.detachMinutes(form.getPmStartTimes()[dayOfWeek]));
		curForm.setPmEndHour(Time.detachHours(form.getPmEndTimes()[dayOfWeek]));
		curForm.setPmEndMinute(Time.detachMinutes(form.getPmEndTimes()[dayOfWeek]));
		
		curForm.setAmStartTime(form.getAmStartTimes()[dayOfWeek]);
		curForm.setAmEndTime(form.getAmEndTimes()[dayOfWeek]);
		curForm.setPmStartTime(form.getPmStartTimes()[dayOfWeek]);
		curForm.setPmEndTime(form.getPmEndTimes()[dayOfWeek]);
		whWorkhoursService.save(curForm);
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Page<WhRegularWorkTimeForm> getPage() {
		return page;
	}


	public void setPage(Page<WhRegularWorkTimeForm> page) {
		this.page = page;
	}


	public WhRegularWorkTimeForm getModel() {
		return form;
	}
	
}
